package com.novel.config;

import com.novel.common.job.*;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;

/**
 * 定时任务配置
 */
@Configuration
public class QuartzConfig {

    /**
     * 把当前时间答应到控制台上
     */
    @Bean
    public JobDetail timeJobDetail() {
        return JobBuilder.newJob(TimeJob.class).storeDurably().build();
    }

    @Bean
    public CronTriggerFactoryBean timeTrigger() {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(timeJobDetail());
        //每隔10秒中执行一次，打印当前时间
        trigger.setCronExpression("*/10 * * * * ?");
        return trigger;
    }

    /**
     * 用户推荐票发放的定时任务
     */
    @Bean
    public JobDetail recommendJobDetail() {
        return JobBuilder.newJob(FreeSendRecommendJob.class)
                .storeDurably()//把job信息持久化的属性 如果设置为true无论关联的trigger是否存在他都会存在;反之，则任何情况都不会存在
                .build();
    }

    @Bean
    public CronTriggerFactoryBean recommendTrigger() {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(recommendJobDetail());
        //每天晚上11点执行一次
        trigger.setCronExpression("0 0 23 * * ?");
        return trigger;
    }


    /**
     * 定时把用户点击量同步到数据库中
     */
    @Bean
    public JobDetail addNovelClickJobDetail() {
        return JobBuilder.newJob(AddNovelClickJob.class)
                .storeDurably()//把job信息持久化的属性 如果设置为true无论关联的trigger是否存在他都会存在;反之，则任何情况都不会存在
                .build();
    }

    @Bean
    public CronTriggerFactoryBean addNovelClickTrigger() {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(addNovelClickJobDetail());
        //10秒同步到数据库中一次
        trigger.setCronExpression("*/10 * * * * ?");
        return trigger;
    }

    /**
     * 定时任务计算作家前一天的稿酬，并发放到作家账户余额中
     */
    @Bean
    public JobDetail clearAuthorIncomeJobDetail() {
        return JobBuilder.newJob(ClearAuthorIncomeJob.class)
                .storeDurably()//把job信息持久化的属性 如果设置为true无论关联的trigger是否存在他都会存在;反之，则任何情况都不会存在
                .build();
    }

    @Bean
    public CronTriggerFactoryBean clearAuthorIncomeTrigger() {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(clearAuthorIncomeJobDetail());
        //每天晚上12点执行
        trigger.setCronExpression("0 0 0 * * ?");
        return trigger;
    }

    /**
     * 定时任务
     * 定时把申请成为作家的用户通过申请
     */
    @Bean
    public JobDetail passApplyAuthorJobDetail() {
        return JobBuilder.newJob(PassApplyAuthorJob.class)
                .storeDurably()
                .build();
    }

    @Bean
    public CronTriggerFactoryBean passApplyAuthorTrigger() {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(passApplyAuthorJobDetail());
        //每隔1天执行一次
        trigger.setCronExpression("0 0 0 1/1 * ?");
        return trigger;
    }

}